<?php
/*
 * generate_db.php
 *
 * Copyright (c) 2007 Marcelo Lira dos Santos
 *
 * Author: Marcelo Lira dos Santos <setanta@gmail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

require_once 'activity.php';
require_once 'activity_collection.php';
require_once 'category_collection.php';

if (!isset($_POST['atividades'])) {
    print "Problemas...";
    return;
}

function get_sqlite_activity_query($activity) {
    $query = "INSERT INTO activities (title,author,description,age," .
             "duration,goals,procedures,evaluation,program,category) " .
             "VALUES ('$activity->title', '$activity->author', " .
             "'$activity->description', '$activity->age', " .
             "'$activity->duration', '$activity->goals', " .
             "'$activity->procedures', '$activity->evaluation', " .
             "'$activity->program', '$activity->category');
";

    return $query;
}

function get_sqlite_category_query($category) {
    $query = "INSERT INTO categories " .
             "VALUES ('$category[0]', '$category[1]', '$category[2]');
";

    return $query;
}


$page = implode('', file('template.html'));
$query = "";

$cat_col = new CategoryCollection();
$act_col = new ActivityCollection();

$categories = $cat_col->get_all_categories();

foreach ($categories as $category) {
    $query = $query . get_sqlite_category_query($category);
}
$query = $query . '
';

$list = $_POST["atividades"];
$selected = $act_col->get_some_activities($list);

foreach($selected as $activity) {
    $query = $query . get_sqlite_activity_query($activity);
}

$struct = implode('', file('structure.sql')) . '
';

$sql = $struct . $query;

$filename = 'atividades.sql';
$fd = fopen('./' . $filename, 'w');
fwrite($fd, $sql);
fclose($fd);

$text  = '<p>Download: <a href="' . $filename . '">' . $filename . '</a></p>
<TEXTAREA COLS=110 ROWS=22 NAME="query">
' . $sql . '</TEXTAREA>';

print str_replace('_CONTENTS', $text, $page);

?>
